package com.netmi.myjectpack.viewmodel;

/**
 * 类描述：
 * 创建人：tgw
 * 创建时间：2020/8/26
 * 修改备注：
 */

import androidx.databinding.Observable;
import androidx.databinding.PropertyChangeRegistry;
import androidx.lifecycle.ViewModel;

/**
 * 自定义viewmodel
 * 视图模型也是可观察的，
 * 与数据绑定库一起使用。
 * A ViewModel that is also an Observable,
 * to be used with the Data Binding Library.
 */
class ObservableViewModel extends ViewModel implements Observable {
    private PropertyChangeRegistry callbacks = new PropertyChangeRegistry();

    @Override
    public void addOnPropertyChangedCallback(
            Observable.OnPropertyChangedCallback callback) {
        callbacks.add(callback);
    }

    @Override
    public void removeOnPropertyChangedCallback(
            Observable.OnPropertyChangedCallback callback) {
        callbacks.remove(callback);
    }

    /**
     * 通知观察者此实例的所有属性已更改。
     * Notifies observers that all properties of this instance have changed.
     */
    void notifyChange() {
        callbacks.notifyCallbacks(this, 0, null);
    }

    /**
     * 通知观察者某个特定属性已更改。的getter
     * *属性，更改应该用@Bindable注释标记到
     * *在BR类中生成一个字段作为fieldId参数。
     * *
     * * @param fieldId为可绑定字段生成BR id。
     *
     * Notifies observers that a specific property has changed. The getter for the
     * property that changes should be marked with the @Bindable annotation to
     * generate a field in the BR class to be used as the fieldId parameter.
     *
     * @param fieldId The generated BR id for the Bindable field.
     */
    void notifyPropertyChanged(int fieldId) {
        callbacks.notifyCallbacks(this, fieldId, null);
    }
}

